jQ(function(){
	var state=getParameter("state");
	if(typeof(state)!='undefined'&&state=='sina'){
		jQ("#register").hide();
		jQ("#sinareg").show();
		jQ("#reg_title").text("首次登录，补全信息");
		jQ("#sinareg_birth").datepicker({
			yearRange : '1950',
			dateFormat:'yy-mm-dd',
			changeMonth:true,
			changeYear:true,
			beforeShow: function(i,e){  
				var z = jQuery(i).closest(".ui-dialog").css("z-index") + 4;  
				e.dpDiv.css('z-index', z);
				e.dpDiv.css('font', "14px \"Trebuchet MS\", sans-serif");
			}
		});
		
		jQ.ajax({
			type: "POST",
			url: "userAjax!getSinaUserInfo.action",
			success:function(data){
				if(data.result){
					var avatarLargeurl=data.avatarLargeurl;
					var name=data.name;
					jQ("#sinareg_head").attr("src",avatarLargeurl);
					jQ("#sinareg_name").val(name);
					var addresscode=data.addresscode;
					var pid=0,cid=0,did=0;
					if(addresscode.length>0){
						if(addresscode.indexOf("_")>0){
							var temparr=addresscode.split("_");
							pid=eval(temparr[0]);
							cid=eval(temparr[1]);
							if(temparr.length>2){
								did=eval(temparr[2]);
							}
						}else{
							pid=eval(addresscode);
						}
					}
					jQ.ajax({
						type: "POST",
						url: "userAjax!loadAllProvinces.action",
						success:function(data){
							if(data.result){
								var phtml="";
								if(pid==0){
									phtml+="<option value='0' type='0'>";
									phtml+="请选择";
									phtml+="</option>";
								}
								var pname,ptype;
								for(var i=0;i<data.provinces.length;i++){
									var province=data.provinces[i];
									var provinceid=province.id;
									var name=province.name;
									var type=province.type;
									if(pid==provinceid){
										pname=name;
										ptype=type;
										phtml+="<option value='"+provinceid+"' type='"+type+"' selected='selected'>";
									}else{
										phtml+="<option value='"+provinceid+"' type='"+type+"'>";
									}
									phtml+=name;
									phtml+="</option>";
								}
								jQ("#provinceselect").empty().append(phtml);
								if(pid==0){
									var chtml,dhtml;
									chtml="<option value='0' type='0'>";
									chtml+="请选择";	
									chtml+="</option>";
									dhtml="<option value='0' type='0'>";
									dhtml+="请选择";	
									dhtml+="</option>";
									jQ("#cityselect").empty().append(chtml);
									jQ("#districtselect").empty().append(dhtml);
									initAreaSelect();
									return;
								}
								if(cid==0){
									if(ptype==0){
										jQ.ajax({
											type: "POST",
											url: "userAjax!loadCityByProvinceid.action",
											data:{
												provinceid:pid,
												type:0
											},
											success:function(data){
												if(data.result){
													var chtml="";
													var dhtml="";
													
													chtml+="<option value='0' type='0'>";
													chtml+="请选择";	
													chtml+="</option>";
													for(var i=0;i<data.cities.length;i++){
														var city=data.cities[i];
														var cityid=city.id;
														var name=city.name;
														var type=city.type;
														if(cityid==cid){
															chtml+="<option value='"+cityid+"' pid='"+pid+"' type='"+type+"' selected='selected'>";
														}else{
															chtml+="<option value='"+cityid+"' pid='"+pid+"' type='"+type+"'>";
														}
														chtml+=name;
														chtml+="</option>";
													}

													dhtml+="<option value='0' type='0'>";
													dhtml+="请选择";
													dhtml+="</option>";
													
													jQ("#cityselect").empty().append(chtml);
													jQ("#districtselect").empty().append(dhtml);
													initAreaSelect();
													return;
												}
											}
										});
									}else{

										chtml+="<option value='0'>";
										chtml+=pname;
										chtml+="</option>";
										jQ("#cityselect").empty().append(chtml);
										jQ.ajax({
											type: "POST",
											url: "userAjax!loadDistrict.action",
											data:{
												cityid:0,
												provinceid:pid
											},
											success:function(data){
												if(data.result){
													var dhtml="";

													if(did==0){
														dhtml+="<option value='0' type='0'>";
														dhtml+="请选择";
														dhtml+="</option>";
													}
													for(var i=0;i<data.districts.length;i++){
														var districts=data.districts[i];
														var districtid=districts.id;
														var name=districts.name;
														var type=districts.type;
														if(districtid==did){
															dhtml+="<option value='"+districtid+"' type='"+type+"' selected='selected'>";
														}else
															dhtml+="<option value='"+districtid+"' type='"+type+"'>";
														dhtml+=name;
														dhtml+="</option>";
													}
													jQ("#districtselect").empty().append(dhtml);
													initAreaSelect();
													return;
												}else{
													alert("数据出错");
												}
											}
										});
									}
								}else{
									jQ.ajax({
										type: "POST",
										url: "userAjax!loadCityByProvinceid.action",
										data:{
											provinceid:pid,
											type:0
										},
										success:function(data){
											if(data.result){
												var chtml="";
												if(cid==0){
													chtml+="<option value='0' type='0'>";
													chtml+="请选择";	
													chtml+="</option>";
												}
												
												for(var i=0;i<data.cities.length;i++){
													var city=data.cities[i];
													var cityid=city.id;
													var name=city.name;
													var type=city.type;
													if(cityid==cid){
														chtml+="<option value='"+cityid+"' pid='"+pid+"' type='"+type+"' selected='selected'>";
													}else{
														chtml+="<option value='"+cityid+"' pid='"+pid+"' type='"+type+"'>";
													}
													chtml+=name;
													chtml+="</option>";
												}
												
												jQ("#cityselect").empty().append(chtml);
												
												jQ.ajax({
													type: "POST",
													url: "userAjax!loadDistrict.action",
													data:{
														cityid:cid,
														provinceid:pid
													},
													success:function(data){
														if(data.result){
															var dhtml="";
															if(did==0){
																dhtml+="<option value='0' type='0'>";
																dhtml+="请选择";
																dhtml+="</option>";
															}
															for(var i=0;i<data.districts.length;i++){
																var districts=data.districts[i];
																var districtid=districts.id;
																var name=districts.name;
																var type=districts.type;
																if(districtid==did){
																	dhtml+="<option value='"+districtid+"' type='"+type+"' selected='selected'>";
																}else{
																	dhtml+="<option value='"+districtid+"' type='"+type+"'>";
																}
																dhtml+=name;
																dhtml+="</option>";
															}
															jQ("#districtselect").empty().append(dhtml);
															initAreaSelect();
															return;
														}else{
															alert("数据出错");
														}
													}
												});
												
											}else{
												alert("数据出错");
											}
										}
									});
								}
							}else{
								alert("数据出错");
							}
						}
					});
				}else{
					clip_tips(data.errormsg);
				}
			}
		});
		jQ("#sinareg_btn").unbind("click").bind("click",submitsinareginfo);
	}else{
		jQ("#register").show();
		jQ("#sinareg").hide();
		var div_box = jQ("#register").find("div");
		jQ.each(div_box,function(i,dat){
			initBulidFun(dat);
		});
		initAddressSelection();
	}

});

function initAddressSelection(){
	jQ.ajax({
		type: "POST",
		url: "userAjax!loadAllProvinces.action",
		success:function(data){
			if(data.result){
				jQ("#preaddressdiv select").eq(0).empty();
				var html="";
				html+="<option value='0' type='0'>";
				html+="请选择";
				html+="</option>";
				for(var i=0;i<data.provinces.length;i++){
					var province=data.provinces[i];
					var id=province.id;
					var name=province.name;
					var type=province.type;
					html+="<option value='"+id+"' type='"+type+"'>";
					html+=name;
					html+="</option>";
				}
				jQ("#preaddressdiv select").eq(0).append(html);
				jQ("#preaddressdiv select").eq(0).change(onProvincesSelect);
			}else{
				alert("数据出错");
			}
		}
	});
}

function initAreaSelect(){
	jQ("#provinceselect").change(onProvincesSelect);
	jQ("#cityselect").change(onCitesSelect);
}

function onProvincesSelect(){
	var cityselect;
	var districtselect;
	if(jQ("#sinareg:visible").length>0){
		cityselect=jQ("#cityselect");
		districtselect=jQ("#districtselect");
	}else{
		cityselect=jQ("#preaddressdiv select").eq(1);
		districtselect=jQ("#preaddressdiv select").eq(2);
	}
	districtselect.empty().append("<option value='0' type='0'>请选择</option>");
	var id=eval(jQ(this).val());
	var type=eval(jQ(this).find("option:selected").attr("type"));
	if(id==0){
		return;
	}else{
		if(type==-1){
			var name=jQ(this).find("option:selected").text();
			cityselect.empty().append("<option type='-1' id='"+id+"'>"+name+"</option>");
			jQ.ajax({
				type: "POST",
				url: "userAjax!loadDistrict.action",
				data:{
					cityid:0,
					provinceid:id
				},
				success:function(data){
					if(data.result){
						districtselect.empty();
						var html="";
						html+="<option value='0' type='0'>";
						html+="请选择";
						html+="</option>";
						for(var i=0;i<data.districts.length;i++){
							var districts=data.districts[i];
							var id=districts.id;
							var name=districts.name;
							var type=districts.type;
							html+="<option value='"+id+"' type='"+type+"'>";
							html+=name;
							html+="</option>";
						}
						districtselect.append(html);
					}else{
						alert("数据出错");
					}
				}
			});
			return;
		}else{
			jQ.ajax({
				type: "POST",
				url: "userAjax!loadCityByProvinceid.action",
				data:{
					provinceid:id,
					type:type
				},
				success:function(data){
					if(data.result){
						cityselect.empty();
						var html="";
						html+="<option value='0' type='0'>";
						html+="请选择";
						html+="</option>";
						for(var i=0;i<data.cities.length;i++){
							var city=data.cities[i];
							var cityid=city.id;
							var name=city.name;
							var type=city.type;
							html+="<option value='"+cityid+"' pid='"+id+"' type='"+type+"'>";
							html+=name;
							html+="</option>";
						}
						cityselect.append(html);
						cityselect.change(onCitesSelect);
					}else{
						alert("数据出错");
					}
				}
			});
		}
	}

}

function onCitesSelect(){
	var id=eval(jQ(this).val()); //cityid
	var pid=eval(jQ(this).find("option:selected").attr("pid"));
	var districtselect;
	if(jQ("#sinareg:visible").length>0){
		districtselect=jQ("#districtselect");
	}else{
		districtselect=jQ("#preaddressdiv select").eq(2);
	}
	jQ.ajax({
		type: "POST",
		url: "userAjax!loadDistrict.action",
		data:{
			cityid:id,
			provinceid:pid
		},
		success:function(data){
			if(data.result){
				districtselect.empty();
				var html="";
				html+="<option value='0' type='0'>";
				html+="请选择";
				html+="</option>";
				for(var i=0;i<data.districts.length;i++){
					var districts=data.districts[i];
					var id=districts.id;
					var name=districts.name;
					var type=districts.type;
					html+="<option value='"+id+"' type='"+type+"'>";
					html+=name;
					html+="</option>";
				}
				districtselect.append(html);
			}else{
				alert("数据出错");
			}
		}
	});
}

function checkEmail(_input,_b,fun){
	var _value = jQ.trim(_input.val());
	if(isEmail(_value)){
		jQ.ajax({
			type: "POST",
			url: "userAjax!checkUser.action",
			async: false,
			data:{
				emailInput : _value
			},
			success:function(data){
				_b.removeClass().empty();
				if(data=="0"){
					_b.addClass("wrong").html("邮箱地址已被使用,请重新输入");
				}else{
					_b.addClass("right");
				}
				if(typeof(fun)=="function")
					fun();
			}
		});
	}else{
		_b.removeClass().addClass("wrong").html("请输入正确的邮箱地址");
		if(typeof(fun)=="function")
			fun();
	}
}
function checkPWD(_input,_b,fun){
	var regEx = /^[a-zA-Z0-9][a-zA-Z0-9_-]{5,20}$/;
	var _value = jQ.trim(_input.val());
	_b.removeClass().empty();
	if(!regEx.test(_value)){
		_b.addClass("wrong").html("数字、英文、下划线及中杠线5到20位的密码");
	}else{
		_b.addClass("right");
	}
	if(typeof(fun)=="function")
		fun();
}
function checkPWD2(_input,_b,fun){
	var pwd = _input.parent().prev("div.password").find("input").val();;
	var _value = jQ.trim(_input.val());
	_b.removeClass().empty();
	if(_value.length<=0){
		_b.addClass("wrong").html("请输入密码");
	}else if(pwd==_value){
		_b.addClass("right");
	}else{
		_b.addClass("wrong").empty().append("两次输入的密码必须一致");
	}
	if(typeof(fun)=="function")
		fun();
}
function checkName(_input,_b,fun){
	var regEx = /^[\u4e00-\u9fa5_a-zA-Z0-9-]{2,20}$/;// 姓名 2-20个
	var _value = jQ.trim(_input.val());
	_b.removeClass().empty();
	if(regEx.test(_value)&&(_value.length>=2&&_value.length<=20)){
		_b.addClass("right");
	}else{
		_b.addClass("wrong").html("姓名由2-20个字符中文、数字、英文、下划线、中杠线组成");
	}
	if(typeof(fun)=="function")
		fun();
}
function checkLive(_input,_b,fun){
	var _value = jQ.trim(_input.val());
	_b.removeClass().empty();
	if(_value.length>15){
		_b.addClass("wrong").html("输入内容不能大于15个字符");
	}else if(_value.length>0){
		_b.addClass("right")
	}
	if(typeof(fun)=="function")
		fun();
}
function checkBlog(_input,_b,fun){
	var _value = jQ.trim(_input.val());
	_b.removeClass().empty();
	if(_value.length>0){
		if(IsURL(_value)){
			_b.addClass("right")
		}else{
			_b.addClass("wrong").html("请输入以http://开头的URL");
		}
	}
	if(typeof(fun)=="function")
		fun();
}
function checkYzm(_input,_b,fun){
	var _value = jQ.trim(_input.val());
	_b.removeClass().empty();
	if(_value.length<0){
		_b.addClass("wrong").html("请输入验证码");
		if(typeof(fun)=="function")
			fun();
	}else{
		jQ.ajax({
			type: "POST",
			url: "userAjax!checkYzm.action",
			async: false,
			data:{
				vcodeInput : _value
			},
			success:function(data){
				if(data=="0"){
					_b.addClass("wrong").html("验证码错误,请重新输入");
				}else{
					_b.addClass("right");
				}
				if(typeof(fun)=="function")
					fun();
			}
		});
	}
}
function initBulidFun(dat){
	var _input = jQ(dat).find("input");
	var _b = jQ(dat).find("b");
	
	if(jQ(dat).hasClass("email")){
		_input.blur(function(){
			checkEmail(_input,_b);
		});
	}else if(jQ(dat).hasClass("password")){
		_input.blur(function(){
			checkPWD(_input,_b);
		});
	}else if(jQ(dat).hasClass("password_repeat")){
		_input.blur(function(){
			checkPWD2(_input,_b);
		});
	}else if(jQ(dat).hasClass("real_name")){
		_input.blur(function(){
			checkName(_input,_b);
		});
	}else if(jQ(dat).hasClass("birthday")){
		_input.datepicker({
			yearRange : '1950',
			dateFormat:'yy-mm-dd',
			changeMonth:true,
			changeYear:true,
			beforeShow: function(i,e){  
				var z = jQuery(i).closest(".ui-dialog").css("z-index") + 4;  
				e.dpDiv.css('z-index', z);
				e.dpDiv.css('font', "14px \"Trebuchet MS\", sans-serif");
			}
		});
	}else if(jQ(dat).hasClass("living")){
		_input.blur(function(){
			checkLive(_input,_b);
		});
	}else if(jQ(dat).hasClass("blog")){
		_input.blur(function(){
			checkBlog(_input,_b);
		});
	}else if(jQ(dat).hasClass("wrap_numb")){
		var _img = jQ(dat).find("img");
		var _a = jQ(dat).find("a");
		_img.click(function(){
			_img.attr("src", jQ("#ctxVal").val()+"/verifycode.do" + "?time=" + (new Date()).valueOf());
		}).click();;
		_a.click(function(){
			_img.attr("src", jQ("#ctxVal").val()+"/verifycode.do" + "?time=" + (new Date()).valueOf());
		});
	}else if(jQ(dat).hasClass("wrap")){
		_input.blur(function(){
			checkYzm(_input,_b);
		});
	}else if(jQ(dat).hasClass("register_btm")){
		var _a = jQ(dat).find("a.rt").click(doSubmit);
	}
}
function doSubmit(){
	
	var div_box = jQ("#register");
	var _email = div_box.find("div.email");
	var _pwd = div_box.find("div.password");
	var _pwd2 = div_box.find("div.password_repeat");
	var _name = div_box.find("div.real_name");
	var _wrap_numb = div_box.find("div.wrap_numb");
	var _wrap = div_box.find("div.wrap");
	var _register_btm = div_box.find("div.register_btm");
	
	var _birthday = div_box.find("div.birthday");
	var _living = div_box.find("div.living");
	var _job = div_box.find("div.job");
	var _edu = div_box.find("div.edu");
	var _blog = div_box.find("div.blog");
	var _sex = div_box.find("div.sex");
	
//	_input
	var _input_email = _email.find("input");
	var _input_pwd = _pwd.find("input");
	var _input_pwd2 = _pwd2.find("input");
	var _input_name = _name.find("input");
	var _input_wrap = _wrap.find("input");
	var _input_birthday = _birthday.find("input");
	var _input_living = _living.find("input");
	var _select_job = _job.find("select");
	var _select_edu = _edu.find("select");
	var _input_blog = _blog.find("input");
	var _input_sex = _sex.find("input[name='sex']:checked");
//	_b
	var _b_email = _email.find("b");
	var _b_pwd = _pwd.find("b");
	var _b_pwd2 = _pwd2.find("b");
	var _b_name = _name.find("b");
	var _b_wrap = _wrap.find("b");
	var _b_living = _living.find("b");
	var _b_blog = _blog.find("b");
			
	checkEmail(_input_email,_b_email,function(){
		checkPWD(_input_pwd,_b_pwd,function(){
			checkPWD2(_input_pwd2,_b_pwd2,function(){
				checkName(_input_name,_b_name,function(){
					checkYzm(_input_wrap,_b_wrap,function(){
//						checkLive(_input_living,_b_living,function(){
//							checkBlog(_input_blog,_b_blog,function(){
								var _flag = false;
								jQ.each(div_box.find("div"),function(i,dat){
									var _b = jQ(dat).find("b")
									if(_b.hasClass("wrong")){
										_flag = true;
									}
								});
								if(_flag){
									jalert("请输入正确的信息");
								}else{
									var birthlong=0;
									if(_input_birthday.val().length>0){
										birthlong=_input_birthday.datepicker("getDate").getTime();
									}
									var pre_address=getPreAddress();
									var suf_address=jQ.trim(jQ("#sufaddress").val());
									if(pre_address.length>0&&pre_address!='0'&&suf_address.length<=0){
										var _b=jQ("#sufaddress").nextAll("b");
										_b.addClass("wrong").html("请输入详细地址");
										return;
									}else if(suf_address.length>50){
										var _b=jQ("#sufaddress").nextAll("b");
										_b.addClass("wrong").html("详细地址请在50个字内");
										return;
									}
									jQ.ajax({
										type : "POST",
										url : "userAjax!register.action",
										data : {
											emailInput : _input_email.val(),
											pwdInput : _input_pwd.val(),
											pwdInput2 : _input_pwd2.val(),
											nameInput : _input_name.val(),
											vcodeInput : _input_wrap.val(),
											preAddress:pre_address,
											sufAddress:suf_address,
											birthdayInput:birthlong
										},
										success : function(data){
											var dataObj=eval("("+data+")");
											if(dataObj.flag==1){
												jQ("#div_msg div.msg p").html(dataObj.result);
												jQ("#div_msg div.msg_btm").html("<input type='button' style='width:auto' value='首页' onclick='gotoLogin()'>");
												jQ("#div_msg").show();
												jQ("#register").hide();
											}else{
												jalert(dataObj.result);
											}
										}
									});
								}
//							});
//						});
					});
				});
			});
		});
	});
}

function getPreAddress(){
	var provinceselect,cityselect,districtselect;
	if(jQ("#sinareg:visible").length>0){
		provinceselect=jQ("#provinceselect");
		cityselect=jQ("#cityselect");
		districtselect=jQ("#districtselect");
	}else{
		provinceselect=jQ("#preaddressdiv select").eq(0);
		cityselect=jQ("#preaddressdiv select").eq(1);
		districtselect=jQ("#preaddressdiv select").eq(2);
	}
	var pval=eval(provinceselect.val());
	if(pval==0){
		return "0";
	}else{
		var ptype=eval(provinceselect.find("option:selected").attr("type"));
		if(ptype==-1){
			var cval="0";
			var dval=eval(districtselect.val());
			if(dval==0){
				return ""+pval;
			}else{
				return pval+"_0_"+dval;
			}
		}else{
			var cval=eval(cityselect.val());
			if(cval==0){
				return ""+pval;
			}else{
				var dval=eval(districtselect.val());
				if(dval==0){
					return pval+"_"+cval;
				}else{
					return pval+"_"+cval+"_"+dval;
				}
			}
		}
	}
}

function submitsinareginfo(){
	
	var headurl=jQ("#sinareg_head").attr("src");
	var sinareg_name=jQ("#sinareg_name").val();
	var sinaregname_b=jQ("#sinareg_name").nextAll("b");
	var nameRegEx = /([A-Za-z0-9_-]*[\u4e00-\u9fa5]+)+/;// 姓名 2-20个
	sinareg_name = jQ.trim(sinareg_name);
	sinaregname_b.removeClass().empty();
	if(!nameRegEx.test(sinareg_name)&&(sinareg_name.length>=2&&sinareg_name.length<=20)){
		sinaregname_b.addClass("wrong").html("姓名由2-20个字符中文、数字、英文、下划线、中杠线组成");
		return;
	}else{
		sinaregname_b.addClass("right");
	}
	var pre_address=getPreAddress();
	var suf_address=jQ.trim(jQ("#address_ext_ipt").val());
	if(pre_address.length>0&&pre_address!='0'&&suf_address.length<=0){
		var _b=jQ("#address_ext_ipt").nextAll("b");
		_b.addClass("wrong").html("请输入详细地址");
	}
	var birthdaylong=jQ("#sinareg_birth").val().length==0?0:jQ("#sinareg_birth").datepicker("getDate").getTime();
	var sexInput=jQ("#sinareg_sex input:checked").val();
	jQ.ajax({
		type : "POST",
		url : "userAjax!sinaregister.action",
		data : {
			sinaheadurl : headurl,
			nameInput : sinareg_name,
			preAddress:pre_address,
			sufAddress:suf_address,
			birthdayInput:birthdaylong,
			sexInput:sexInput
		},
		success : function(data){
			if(data.result){
				jalert("登录成功");
				if(typeof(data.fwurl)!='undefined'&&data.fwurl.length>0){
					window.location.href=data.fwurl;
				}else
					window.location.href=jQ("#ctxVal").val()+"/myclip/home.html";
			}else{
				alert(dataObj.errormsg);
			}
		}
	});
}

function gotoLogin(){
	window.location.href=jQ("#ctxVal").val()+"/index.html";
}
function getParameter(name){
    var paramStr=location.search;
    if(paramStr.length==0)return null;
    if(paramStr.charAt(0)!='?')return null;
    paramStr=decodeURIComponent(paramStr);
    paramStr=paramStr.substring(1);
    if(paramStr.length==0)return null;
    var params=paramStr.split('&');
    var p = null;
    for(var i=0;i<params.length;i++){
        if(params[i].indexOf(name) >= 0){
            p = params[i].split('=');
            p = p[1];
        }
    }
    return p;
}
function jalert(info,LFun){
	jQ("#ALERT p").text(info);
	if(LFun!=null)jQ("#ALERT").bind("LFun",LFun);
	jQ("#ALERT").dialog("option","buttons",{
		"确定" : function(){
			jQ(this).trigger("LFun");
			jQ(this).dialog("close");
		}
	});
	jQ("#ALERT").parent().css('font', "14px \"Trebuchet MS\", sans-serif");
	jQ("#ALERT").dialog('open');
}
function IsURL(str_url) {
	var strRegex = "^(http://)+(([0-9a-zA-Z_]+[\.]+[0-9a-zA-Z_])?([0-9a-zA-Z_]))+[\.]+([a-zA-Z]{2,3})+.+$";
	var re = new RegExp(strRegex);
	return re.test(str_url);
}